package io.gitee.zhangbinhub.acp.cloud.server.helloworld;

import io.gitee.zhangbinhub.acp.cloud.annotation.AcpCloudAtomApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestClient;

import java.time.Duration;

/**
 * @since JDK 17
 */
@AcpCloudAtomApplication
public class HelloWorldApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }

    @Bean("customerRestClientTest")
    @LoadBalanced
    public RestClient.Builder restClient(FeignHttpClientProperties feignHttpClientProperties) {
        return RestClient.builder()
                .requestFactory(
                        ClientHttpRequestFactoryBuilder.httpComponents().withCustomizer(customizer -> {
                            customizer.setConnectTimeout(Duration.of(
                                    feignHttpClientProperties.getHc5().getConnectionRequestTimeout(),
                                    feignHttpClientProperties.getHc5().getConnectionRequestTimeoutUnit().toChronoUnit()
                            ));
                            customizer.setConnectionRequestTimeout(Duration.of(
                                    feignHttpClientProperties.getHc5().getConnectionRequestTimeout(),
                                    feignHttpClientProperties.getHc5().getConnectionRequestTimeoutUnit().toChronoUnit()
                            ));
                            customizer.setReadTimeout(Duration.of(
                                    feignHttpClientProperties.getHc5().getSocketTimeout(),
                                    feignHttpClientProperties.getHc5().getSocketTimeoutUnit().toChronoUnit()
                            ));
                        }).build()
                );
    }

}
